home *** CD-ROM | disk | FTP | other *** search
- Path: news.halcyon.com!usenet
- From: normanb@halcyon.com (Norm Bryar)
- Newsgroups: comp.lang.c++
- Subject: Re: 'chained': It would be nice if...
- Date: Thu, 18 Apr 1996 16:47:38 GMT
- Organization: Northwest Nexus Inc.
- Message-ID: <4l5rlt$dkp@news.halcyon.com>
- References: <31756DEC.5215@zurich.ibm.com> <317505E6.7F7F@cs.tu-berlin.de>
- NNTP-Posting-Host: blv-pm3-ip12.halcyon.com
- X-Newsreader: Forte Free Agent 1.0.82
-
- Roman Lechtchinsky <wolfro@cs.tu-berlin.de> wrote:
-
- >Keith Whittingham wrote:
- >>
- >> Three or four times I've find myself needing a construct which I
- >> think is missing from C++ (or at least is inaccessable).
- >>
- >> A keyword, say 'chained', to have a similar effect to that of
- >> the virtual destructor. e.g.
- >>
- >> class Base
- >> {
- >> public:
- >> virtual void vMember(void);
- >> chained void cMember(void);
- >> };
- >>
- >> class Derived:
- >> public Base
- >> {
- >> public:
- >> void vMember(void);
- >> void cMember(void);
- >> };
- >>
- >> Calling Derived::vMember() executes the code contained in the body
- >> Derived::vMember() whereas calling Derived::cMember() would
- >> execute the code contained in the body Base::cMember() and then
- >> the code contained in Derived::cMember().
-
- >How about:
-
- >class Base
- >{
- >public:
- > void cMember(void)
- > {
- > Base_cMember();
- > Derived_cMember();
- > }
- >protected:
- > virtual void Derived_cMember();
- >private:
- > void Base_cMember();
- >};
-
- >Every time cMember is called the unique private code in Base_cMember is
- >executed. Then Derived_cMember is called which can be overridden in derived
- >classes.
-
- >Bye
-
- >Roman
-
- This is fine when the hierarchy is two classes deep. But
- Derived_Derived overrides Derived_cMember() and now cMember calls
- Base_cMember() then Derived_Derived::Derived_cMember. The
- Derived::Devied_cMember is skipped.
-
- The proposed "chained" should probably have two forms: call-before and
- call-after. While it does remove the possibility someone will forget
- to call the base-class implementation, it also ties their hands as to
- whether they do some pre-processing or post-processing, try-catch
- blocks, etc. It also may have problems in multiple, virtual
- inheritence hierarchies; if D derives from B and C which both derive
- from A, how is the chain supposed to travel?
-
- I think you're only recourse is to publish in the header "You'd be a
- fool to not call the base-class implementation."
-
- I can sympathize, however; I've wanted to force class designers to
- call my base methods, too.
-
- --Norm
-
-